import "reflect-metadata"; // 确保这是第一个导入，不然inversify无法使用

import { myContainer } from "./inversify/inversify.config";
import { CONFIG_NAMES } from "./inversify/CONFIG_NAMES";
import { Container } from "inversify";
import { GuiService } from "./service/GuiService";
import { ConfigService } from "./service/ConfigService";
import { FormulaService } from "./service/FormulaService";
import { CustomFunctionService } from "./service/CustomFunctionService";
import { IService } from "./interface/IService";

/**
 * 客户端可以通过这两个函数实现启动和停止
 */
interface IContainer_ex extends Container {
  start: () => Promise<void>;
  stop: () => Promise<void>;
}

if ((myContainer && !("start" in myContainer)) || !("stop" in myContainer)) {
  const serviceList: IService[] = [
    myContainer.get(GuiService),
    myContainer.get(ConfigService),
    myContainer.get(FormulaService),
    myContainer.get(CustomFunctionService),
  ];
  //@ts-ignore
  myContainer.start = async () => {  
    const loadPromises1 = serviceList.map((service) => service.load());
    await Promise.all(loadPromises1);
    const loadPromises2 = serviceList.map((service) => service.start());
    await Promise.all(loadPromises2);
  };
  //@ts-ignore
  myContainer.stop = async () => {
    const loadPromises3 = serviceList.map((service) => service.stop());
    await Promise.all(loadPromises3);
    const loadPromises4 = serviceList.map((service) => service.unload());
    await Promise.all(loadPromises4);
  };
}

export { CONFIG_NAMES };
export { FormulaService } from "./service/FormulaService";
export { ValueType } from "./interface/IFormulaService";
export { ConfigService } from "./service/ConfigService";
export { GuiService } from "./service/GuiService";
export const container: IContainer_ex = myContainer as IContainer_ex;
export { HyperFormula } from "hyperformula";
